﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using VOL.Core.DBManager;
using VOL.Core.Extensions;
using VOL.Entity.DomainModels;

namespace VOL.WebApi.Controllers.MyTable.TaskList
{
    [Route("api/backTask")]
    [ApiController]
    public class backTaskController : Controller
    {
        [HttpGet, Route("cpdj_task_api"), AllowAnonymous]//产品定价取SAPBOM和价格后台job
        public  dynamic test_task_api()
        {
            string s_url = "http://10.100.150.18:8181/api/cw_djlc_wlzb/get_sap_bom?P_DATUB_OUT={0}&P_MATNR_OUT={1}&newbomnumber={2}&cw_djlc_djzb_id={3}";//取BOM
            string ddtime = DateTime.Now.ToString("yyyyMMdd").ToString();
            List<cw_djlc_djzb> cw_djlc_djzb_list = DBServerProvider.DbContext.Set<cw_djlc_djzb>().Where(x=> x.synchronizationbomstatus == 0 && x.synchronizationpricestatus == 0).ToList();
            Hashtable rtn_hst = new Hashtable();
            for (int i = 0; i < cw_djlc_djzb_list.Count; i++)
            {
                //Console.WriteLine(string.Format(s_url, ddtime, cw_djlc_djzb_list[i].newmaterialnum, cw_djlc_djzb_list[i].newbomnumber,cw_djlc_djzb_list[i].cw_djlc_djzb_id));
                if (cw_djlc_djzb_list[i].synchronizationbomstatus != 1)
                {
                    string aa = string.Format(s_url, ddtime, cw_djlc_djzb_list[i].newmaterialnum, cw_djlc_djzb_list[i].newbomnumber, cw_djlc_djzb_list[i].cw_djlc_djzb_id);
                    var request = (HttpWebRequest)WebRequest.Create(aa);
                    request.Method = "POST";
                    request.ContentType = "application/json;charset=UTF-8";
                    var response = (HttpWebResponse)request.GetResponse();
                    var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
                    if (responseString.Equals("未查询到该物料BOM") || responseString.Equals("物料不存在"))
                    {
                        cw_djlc_djzb_list[i].synchronizationbomstatus = 0;
                        cw_djlc_djzb_list[i].is_synchronized_bom = 0;
                    }
                    else
                    {
                        cw_djlc_djzb_list[i].synchronizationbomstatus = 1;
                        cw_djlc_djzb_list[i].is_synchronized_bom = 1;
                    }
                    cw_djlc_djzb_list[i].is_synchronization_status = 1;
                    DBServerProvider.DbContext.Update(cw_djlc_djzb_list[i]);
                    rtn_hst.Add("BOM-" + cw_djlc_djzb_list[i].newmaterialnum + "-" + cw_djlc_djzb_list[i].cw_djlc_djzb_id, responseString);
                }
            }

            string p_url = "http://10.100.150.18:8181/api/cw_djlc_wlzb/get_sap_price_tou?P_MATNR={0}&bomnumber={1}";//取价格
            for (int i = 0; i < cw_djlc_djzb_list.Count; i++)
            {
                //Console.WriteLine(string.Format(s_url, ddtime, cw_djlc_djzb_list[i].newmaterialnum, cw_djlc_djzb_list[i].newbomnumber,cw_djlc_djzb_list[i].cw_djlc_djzb_id));
                if (cw_djlc_djzb_list[i].synchronizationpricestatus != 1)
                {
                    string aa = string.Format(p_url, cw_djlc_djzb_list[i].newmaterialnum, cw_djlc_djzb_list[i].newbomnumber);
                    var request = (HttpWebRequest)WebRequest.Create(aa);
                    request.Method = "POST";
                    request.ContentType = "application/json;charset=UTF-8";
                    var response = (HttpWebResponse)request.GetResponse();
                    var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
                    if (responseString.ToInt16() == 0)
                    {
                        cw_djlc_djzb_list[i].synchronizationpricestatus = 0;
                    }
                    else
                    {
                        cw_djlc_djzb_list[i].synchronizationpricestatus = 1;
                    }
                    DBServerProvider.DbContext.Update(cw_djlc_djzb_list[i]);

                    rtn_hst.Add("PRICE-" + cw_djlc_djzb_list[i].newmaterialnum + "-" + cw_djlc_djzb_list[i].cw_djlc_djzb_id, responseString);
                }
            }


            DBServerProvider.DbContext.SaveChanges();

            for (int i = 0; i < cw_djlc_djzb_list.Count; i++)
            {
                batchUpdateOfJgwh(cw_djlc_djzb_list[i].cw_djlc_djzb_id.ToString());
            }
            return rtn_hst; 
            //return "00";
        } 
        private void batchUpdateOfJgwh(string cw_djlc_djzb_id)
        {
            var db = DBServerProvider.DbContext;
            Guid djlcId = Guid.Parse(cw_djlc_djzb_id);
            //status 0启用，1不启用
            List<cw_djlc_wljgwh> list = db.Set<cw_djlc_wljgwh>().Where(x => x.cw_djlc_djzb_id == djlcId).ToList();
            var dbcw_djlc_wljgwh = db.Set<cw_djlc_wljgwh>();
            var dbcw_djlc_wlzb = db.Set<cw_djlc_wlzb>();
            var dbcw_djlc_jgwh = db.Set<cw_djlc_jgwh>();
            foreach (var item in list)
            {
                Guid wlzbId = (Guid)item.cw_djlc_wlzb_id;
                cw_djlc_wlzb wlzb = dbcw_djlc_wlzb.Where(x => x.cw_djlc_wlzb_id == wlzbId).ToList().FirstOrDefault();
                string codename = "";
                if (wlzb != null)
                {
                    codename = wlzb.codename;
                }
                //cw_djlc_jgwh jgwh = dbcw_djlc_jgwh.Where(x => x.codename == codename && x.status == 0 && (x.effectivedatestart!=null && x.effectivedatestart.Value.Year == pricestart.Year) &&(x.effectivedateend != null && x.effectivedateend.Value.Year == priceend.Year)).ToList().FirstOrDefault();
                cw_djlc_jgwh jgwh = dbcw_djlc_jgwh.Where(x => x.codename == codename && x.status == 0).ToList().FirstOrDefault();
                if (jgwh != null)
                {
                    if (jgwh.effectivedatestart != null && wlzb.priceeffectivedatestart!=null && jgwh.effectivedatestart.Value.Year==wlzb.priceeffectivedatestart.Value.Year && jgwh.effectivedateend !=null && wlzb.priceeffectivedateend!=null && jgwh.effectivedateend.Value.Year == wlzb.priceeffectivedateend.Value.Year)
                    {
                        item.cw_djlc_jgwh_id = jgwh.cw_djlc_jgwh_id;
                    }
                }
                db.Update(item);
            } 
            db.SaveChanges();
        }
    }
}
